﻿//GENERAL VISUAL CONTROL CONFIGURATION LOGIC

//public methods
var SetVCConfig;
var GetVCConfig;
var restoreColumns;

//public variables
var columnNames = [];
var columnModel = [];

var unSortables = [];

//doc ready
$(document).ready(function () {

    SetVCConfig = function () {
        var grid = jQuery("#list");
        var gridconfig = new Object();
        var outcome = '';
        gridconfig.colNames = grid[0].p.colNames;
        gridconfig.colModel = grid[0].p.colModel;
        gridconfig = pearConfig(gridconfig);
        //var test = window.location.replace(/^(?:\/\/|[^\/]+)*\//, "");
        //var test = window.location.pathname;
        gridconfigtype = window.location.pathname;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/SetVCConfig",
            data: "{vc:'" + gridconfigtype + "', json:" + JSON.stringify(gridconfig) + "}",
            dataType: "json",
            success: function (data, textStatus) {
                outcome = 'success';
            },
            error: function (data, textStatus) {
                outcome = 'failed';
            }
        });
    }

    GetVCConfig = function () {
        gridconfigtype = window.location.pathname;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/GetVCConfig",
            data: "{vc:'" + gridconfigtype + "'}",
            async: false,
            dataType: "json",
            success: function (data, textStatus) {
                var msg = jQuery.parseJSON(data.d);
                if (msg.Message != null) {
                    var gridconfig = cyclereadConfig(jQuery.parseJSON(msg.Message));
                    columnNames = gridconfig.colNames;
                    columnModel = gridconfig.colModel;
                }
            },
            error: function (data, textStatus) {
                outcome = 'failed';
            }
        });
    }

    cyclereadConfig = function (gridconfig) {
        unSortables = [];
        for (var i = 0; i < gridconfig.colModel.length; i++) {
            if (gridconfig.colModel[i].sortable == false) {
                unSortables.push({ name: gridconfig.colModel[i].name, idx: i });
            }
            if (gridconfig.colModel[i].name == 'cb') {
                gridconfig.colNames.splice(i, 1);
                gridconfig.colModel.splice(i, 1);
                i--;
            }
        }
        return gridconfig;
    }

    pearConfig = function (gridconfig) {
        for (var i = 0; i < gridconfig.colModel.length; i++) {
            var testidx = getUnsortableIndex(gridconfig.colModel[i].name);
            if ((testidx > -1) && (testidx != i)) {
                i = gridconfig.colModel.length;
                gridconfig = undefined;
                break;
            }
            else {
                if (gridconfig.colModel[i].name == 'cb') {
                    gridconfig.colNames.splice(i, 1);
                    gridconfig.colModel.splice(i, 1);
                    i--;
                }
            }
        }
        return gridconfig;
    }
    getUnsortableIndex = function (name) {
        var retVal = -1;
        $.each(unSortables, function (idx, val) {
            if (val['name'] == name) {
                retVal = val['idx'];
            }
        });
        return retVal;
    }


    restoreColumns = function () {
        var resp = confirm("Are you sure you want to restore this grid to it's default state?")
        if (resp) {
            $('#loaderwrapper').show();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Service/Services.aspx/KillVCConfig",
                data: "{vc:'" + gridconfigtype + "'}",
                async: false,
                dataType: "json",
                success: function (data, textStatus) {
                    var msg = jQuery.parseJSON(data.d);
                    if (msg.Message != null) {
                        var gridconfig = jQuery.parseJSON(msg.Message);
                        for (var i = 0; i < gridconfig.colModel.length; i++) {
                            if (gridconfig.colModel[i].name == 'cb') {
                                gridconfig.colNames.splice(i, 1);
                                gridconfig.colModel.splice(i, 1);
                                i = gridconfig.colModel.length + 1;
                            }
                        }
                        columnNames = gridconfig.colNames;
                        columnModel = gridconfig.colModel;
                        $("#list").jqGrid('GridUnload');
                        $("#list").jqGrid('GridDestroy');
                        CreateGrid();
                    }
                    $("#list").trigger("reloadGrid");
                    $('#loaderwrapper').hide();
                },
                error: function (data, textStatus) {
                    outcome = 'failed';
                    $('#loaderwrapper').hide();
                }
            });
        }
    }
}); 